package com.buddy.sds.auth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.buddy.sds.auth.entity.SystemDict;
import com.buddy.sds.auth.entity.SystemDictItem;
import com.buddy.sds.auth.service.ISystemDictItemService;
import com.buddy.sds.auth.service.ISystemDictService;
import com.buddy.sds.common.RestApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/auth/dict")
public class SystemDictController {

    @Autowired
    private ISystemDictService dictService;

    @Autowired
    private ISystemDictItemService dictItemService;

    @GetMapping("/getDictList")
    public RestApiResponse getDictList(Page page){

        //dictService.getDictCascadeList(page);

        dictService.page(page);

        return new RestApiResponse(0,"",page);
    }

    @GetMapping("/getDictItemDetail")
    public RestApiResponse getDictItemDetail(@RequestParam("dictIdent")String dictIdent,@RequestParam("itemKey")String itemKey){

        SystemDictItem item = dictItemService.getByIdentAndKey(dictIdent,itemKey);

        return new RestApiResponse(item != null ? 0 : -1 , item != null ? "":"字典项不存在",item);

    }

    @GetMapping("/getDictItemList")
    public RestApiResponse getDictItemList(@RequestParam("dictId")String dictId){

        List<SystemDictItem> items = dictItemService.listByDict(dictId);

        return new RestApiResponse(0,"",items);
    }

    @GetMapping("/getDictItemListByDictIdent")
    public RestApiResponse getDictItemListByDictIdent(@RequestParam("dictIdent")String dictIdent){

        List<SystemDictItem> items = dictItemService.listByDictIent(dictIdent);

        return new RestApiResponse(0,"",items);
    }

    @GetMapping("/getDict")
    public RestApiResponse getDict(@RequestParam("dictId")String dictId){

        SystemDict dict = dictService.getById(dictId);

        return new RestApiResponse(dict == null ? -1 : 0 ,dict == null ? "字典不存在": "" , dict);
    }


    @GetMapping("/getDictItem")
    public RestApiResponse getDictItem(@RequestParam("itemId")String itemId){

        SystemDictItem item = dictItemService.getById(itemId);

        return new RestApiResponse(item == null ? -1 : 0 ,item == null ? "字典项不存在": "" , item);
    }

    @PutMapping("/updateDict")
    public RestApiResponse updateDict(@RequestBody SystemDict dict){

        int status = -1;
        String message = null;

        do{
            if(StringUtils.isEmpty(dict.getDictId())){
                message = "字典id为空";

                break;
            }

            SystemDict oldDict = dictService.getById(dict.getDictId());

            if(oldDict == null){

                message = "字典不存在";
                break;
            }

            if(StringUtils.isEmpty(dict.getDictIdent())){

                message = "字典唯一标识为空";
                break;
            }

            if(!oldDict.getDictIdent().equals(dict.getDictIdent())){

                if(dictService.exisitIdent(dict.getDictIdent())){
                    message = "字典唯一标识已存在";
                    break;
                }
            }

            if(StringUtils.isEmpty(dict.getDictName())){
                message = "字典名称为空";
                break;
            }

            if(dictService.updateById(dict)){

                status = 0;
                message = "修改成功";
            }else{
                message = "修改失败";
            }


        }while (false);

        return new RestApiResponse(status,message,null);
    }



    @PostMapping("/addDict")
    public RestApiResponse addDict(@RequestBody SystemDict dict){

        int status = -1;
        String message = null;

        do{
            if(StringUtils.isEmpty(dict.getDictIdent())){

                message = "字典唯一标识为空";
                break;
            }

            if(dictService.exisitIdent(dict.getDictIdent())){
                message = "字典唯一标识已存在";
                break;
            }

            if(StringUtils.isEmpty(dict.getDictName())){
                message = "字典名称为空";
                break;
            }

            if(dictService.save(dict)){

                status = 0;
                message = "添加成功";
            }else{
                message = "添加失败";
            }


        }while (false);

        return new RestApiResponse(status,message,null);
    }



    @PostMapping("/addDictItem")
    public RestApiResponse addDictItem(@RequestBody SystemDictItem item){

        int status = -1;
        String message = null;

        do{
            if(StringUtils.isEmpty(item.getItemKey())){

                message = "字典项唯一标识为空";
                break;
            }

            if(StringUtils.isEmpty(item.getItemDict())){
                message = "字典标识为空";
                break;
            }

            if(StringUtils.isEmpty(item.getItemName())){
                message = "字典名称为空";
                break;
            }

            if(dictItemService.existsKey(item.getItemKey())){
                message = "字典key已存在";
                break;
            }

            if(dictItemService.save(item)){

                status = 0;
                message = "添加成功";
            }else{
                message = "添加失败";
            }


        }while (false);

        return new RestApiResponse(status,message,null);
    }


    @PutMapping("/updateDictItem")
    public RestApiResponse updateDictItem(@RequestBody SystemDictItem item){

        int status = -1;
        String message = null;


        do{

            if(StringUtils.isEmpty(item.getItemId())){

                message = "字典项id为空";
                break;
            }


            SystemDictItem oldItem = dictItemService.getById(item.getItemId());

            if(oldItem == null){
                message = "字典不存在";
                break;
            }

            if(StringUtils.isEmpty(item.getItemKey())){

                message = "字典项唯一标识为空";
                break;
            }

            if(StringUtils.isEmpty(item.getItemDict())){
                message = "字典标识为空";
                break;
            }

            if(dictService.getById(item.getItemDict()) == null){
                message = "字典标识不存在";
                break;
            }

            if(StringUtils.isEmpty(item.getItemName())){
                message = "字典名称为空";
                break;
            }

            if(!oldItem.getItemKey() .equals(item.getItemKey())){

                if(dictItemService.existsKey(item.getItemKey())){
                    message = "字典key已存在";
                    break;
                }
            }

            if(dictItemService.updateById(item)){

                status = 0;
                message = "添加成功";
            }else{
                message = "添加失败";
            }


        }while (false);

        return new RestApiResponse(status,message,null);
    }



    /**
     * 级联删除字典
     * @param dictId
     * @return
     */
    @DeleteMapping("/deleteDict")
    public RestApiResponse deleteDict(@RequestParam("dictId")String dictId){
        dictService.deleteCascade(dictId);

        return new RestApiResponse(0,"删除成功",null);
    }

    @DeleteMapping("/deleteDictItem")
    public RestApiResponse deleteDictItem(@RequestParam("itemId")String itemId){

        if(dictItemService.removeById(itemId)){

            return new RestApiResponse(0,"删除成功",null);
        }else{

            return new RestApiResponse(-1,"删除失败",null);
        }

    }
}
